﻿using Common.View.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows;
using Common.Tool.Models;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace Common.View.Controls
{
    public class ExRichTextBox : RichTextBox
    {
        public ExRichTextBox() : base()
        {
            IsReadOnly = true;
            RichDocument = new RichDocument() { Text = new System.Collections.ObjectModel.ObservableCollection<string>(), Brush = new List<System.Windows.Media.Brush>() };
            RichDocument.Text.CollectionChanged += (sender, e) =>
            {
                Application.Current.Dispatcher.Invoke((Action)delegate
                {
                    if (RichDocument.Text.Count == 0)
                    {
                        while (this.Document.Blocks.Count > 0)
                        {
                            this.Document.Blocks.Remove(this.Document.Blocks.FirstBlock);
                        }
                        return;
                    }
                    var p = new Paragraph(); // Paragraph 类似于 html 的 P 标签
                    var r = new Run(RichDocument.Text.Last()); // Run 是一个 Inline 的标签
                    p.Inlines.Add(r);
                    p.Foreground = RichDocument.Brush.Last();//设置字体颜色
                    p.Margin = new System.Windows.Thickness(3, 6, 3, 3);
                    //添加文本
                    this.Document.Blocks.Add(p);
                    //todo 验证
                    if (this.Document.Blocks.Count > 10000)
                    {
                        this.Document.Blocks.Remove(this.Document.Blocks.FirstBlock);
                    }
                    //滚动到末尾
                    this.ScrollToVerticalOffset(this.Document.Blocks.Count * 200);
                });
            };
        }
        public ExRichTextBox(RichDocument document) : base()
        {
            IsReadOnly = true;
            RichDocument = document;
            //加载日志
            if (RichDocument.Text.Any())
            {
                this.Document.Blocks.Remove(this.Document.Blocks.FirstBlock);
                for (int i = 0; i < RichDocument.Text.Count; i++) 
                {
                    Application.Current.Dispatcher.Invoke((Action)delegate
                    {
                        var p = new Paragraph(); // Paragraph 类似于 html 的 P 标签
                        var r = new Run(RichDocument.Text[i]); // Run 是一个 Inline 的标签
                        p.Inlines.Add(r);
                        p.Foreground = RichDocument.Brush[i];//设置字体颜色
                        p.Margin = new System.Windows.Thickness(3, 6, 3, 3);
                        //添加文本
                        this.Document.Blocks.Add(p);
                        //todo 验证
                        if (this.Document.Blocks.Count > 10000)
                        {
                            this.Document.Blocks.Remove(this.Document.Blocks.FirstBlock);
                        }
                        //滚动到末尾
                        this.ScrollToVerticalOffset(this.Document.Blocks.Count * 200);
                    });
                }
            }
            RichDocument.Text.CollectionChanged += (sender, e) =>
            {
                //加载日志
                Application.Current.Dispatcher.Invoke((Action)delegate
                {
                    if (RichDocument.Text.Count == 0)
                    {
                        while (this.Document.Blocks.Count > 0)
                        {
                            this.Document.Blocks.Remove(this.Document.Blocks.FirstBlock);
                        }
                        return;
                    }
                    var p = new Paragraph(); // Paragraph 类似于 html 的 P 标签
                    var r = new Run(RichDocument.Text.Last()); // Run 是一个 Inline 的标签
                    p.Inlines.Add(r);
                    p.Foreground = RichDocument.Brush.Last();//设置字体颜色
                    p.Margin = new System.Windows.Thickness(3, 6, 3, 3);
                    //添加文本
                    this.Document.Blocks.Add(p);
                    //todo 验证
                    if (this.Document.Blocks.Count > 10000)
                    {
                        this.Document.Blocks.Remove(this.Document.Blocks.FirstBlock);
                    }
                    //滚动到末尾
                    this.ScrollToVerticalOffset(this.Document.Blocks.Count * 200);
                });
            };
        }
        public RichDocument RichDocument = new RichDocument();
    }
}
